import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFileChooser;

public class SimpleTest {

	public static void main(String[] args) {
		File file1 = getFileToOpen(args, 0, "input1.tsv", "Choose the input1.tsv file");
		BufferedReader brInput1;
		if (file1 == null) {
			return;
		} else {
			try {
				brInput1 = new BufferedReader(new FileReader(file1));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return;
			}
		}
		File file2 = getFileToOpen(args, 1, "input2.tsv", "Choose the input2.tsv file");
		BufferedReader brInput2;
		if (file2 == null) {
			return;
		} else {
			try {
				brInput2 = new BufferedReader(new FileReader(file2));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return;
			}
		}
		File file3 = getFileToOpen(args, 1, "locations.tsv", "Choose the locations.tsv file");
		BufferedReader brLocations;
		if (file3 == null) {
			return;
		} else {
			try {
				brLocations = new BufferedReader(new FileReader(file3));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return;
			}
		}

		try {
			BufferedWriter bw1 = new BufferedWriter(new FileWriter(new File(file1.getAbsolutePath() + ".res")));

			int resMax = 0;
			String line;
			String lineMax = null;
			int res;
			MinorityVariants calculo = new MinorityVariants();
			while ((line = brInput1.readLine()) != null) {
				line = line.trim();
				String[] inputs = line.split("\t");
				res = calculo.calculateConfidence(inputs[2] + " " + inputs[3] + " " + inputs[4]);
				if (res > resMax) {
					resMax = res;
					lineMax = line;
				}
				bw1.write(line + "\t" + res);
				bw1.newLine();
			}

			BufferedWriter bw2 = new BufferedWriter(new FileWriter(new File(file2.getAbsolutePath() + ".res")));

			while ((line = brInput2.readLine()) != null) {
				line = line.trim();
				String[] inputs = line.split("\t");
				res = calculo.calculateConfidence(inputs[2] + " " + inputs[3] + " " + inputs[4]);
				if (res > resMax) {
					resMax = res;
					lineMax = line;
				}
				bw2.write(line + "\t" + res);
				bw2.newLine();
			}
			System.out.println(resMax);
			System.out.println(lineMax);
			bw1.flush();
			bw2.flush();
			bw1.close();
			bw2.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static File getFileToOpen(String[] args, int pos, String fileNameDefault, String fileChooserMsg) {
		String fileName;
		if (args.length > pos) {
			fileName = args[pos];
			return new File(fileName);
		}
		fileName = fileNameDefault;
		File file = null;
		if (fileName != null) {
			file = new File(fileName);
			if (file.canRead()) {
				return file;
			}
		}
		JFileChooser fc = new JFileChooser();
		fc.setDialogTitle(fileChooserMsg);
		if (file != null) {
			fc.setSelectedFile(file);
		}
		int returnVal = fc.showOpenDialog(null);
		if (returnVal != JFileChooser.APPROVE_OPTION) {
			return null;
		}
		return fc.getSelectedFile();
	}

}
